package com.bdqn.easybuy1.service.news;

import com.bdqn.easybuy1.dao.NewsDao;
import com.bdqn.easybuy1.dao.impl.NewsDaoImpl;
import com.bdqn.easybuy1.entities.News;
import com.bdqn.easybuy1.param.NewsParams;
import com.bdqn.easybuy1.utils.JDBCUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class NewsServiceImpl implements NewsService {

    @Override
    public void addNews(News news) {

    }

    @Override
    public News findNewsById(String parameter) {
        News news = null;
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            NewsDao newsDao = new NewsDaoImpl();
            news = newsDao.getNewsById(connection, Integer.parseInt(parameter));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return news;
        }
    }

    @Override
    public void deleteNews(String parameter) {

    }

    /**
     * 分页查询新闻列表
     *
     * @param param 分页参数
     * @return
     */
    @Override
    public List<News> queryNewsList(NewsParams param) {
        List<News> newsList = null;
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            NewsDao newsDao = new NewsDaoImpl();
            newsList = newsDao.getNewsList(connection, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
        }
        return newsList;
    }

    @Override
    public Integer queryNewsCount(NewsParams param) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            NewsDao newsDao = new NewsDaoImpl();
            count = newsDao.getNewsCount(connection,param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count;
        }
    }
}
